$(function () {

    var content = '';

    layui.use(['form','code'], function(){
        if($('#store').length > 0){
            form = layui.form;
            layui.code();

            getStoreList(form);

            // change 事件
            /*form.on('select(store)', function(data){
                //getStoreList();
                form.render('select');
            });*/
        }
    });

    layui.use('layedit', function(){
        var layedit = layui.layedit;
        layedit.set({
            uploadImage: {
                url: '/admin/Upload/layuiUpload' //接口url
                ,type: 'post' //默认post
                ,done: function(res){
                    //上传完毕回调
                    if(res.status == 1){
                        //
                    }
                }
                ,error: function(){
                    //请求异常回调
                }
            }
        });

        content = layedit.build('content'); //建立编辑器
        layedit.setContent(content, $('#content').text());
    });

    layui.use(['form','layer'], function(){
        $ = layui.jquery;
        var form = layui.form,
            layer = layui.layer;

        //自定义验证规则
        form.verify({
            title: function(value){
                if(value == ''){
                    return '标题不能为空';
                }
            }
        });

        //监听提交
        form.on('submit(add)', function(data){
            var param = data.field;

            if(!param.type || param.type != 0){
                // 单个店铺
                if(param.storeId == ''){
                    layer.msg("请选择店铺");
                    return false;
                }
                param.type = 1;
            }else{
                // 所有店铺
                param.storeId = '';
            }

            var layedit = layui.layedit;
            param.content = layedit.getContent(content);

            //发异步，把数据提交给php
            $.ajax({
                url:save_url,
                data:param,
                type:"Post",
                dataType:"json",
                success:function(data){
                    if(data.status == 1){
                        layer.msg("添加成功");
                        var index = parent.layer.getFrameIndex(window.name);
                        //关闭当前frame
                        parent.layer.close(index);
                        parent.location.reload();
                    }
                },
                error:function(data){
                    layer.msg("服务器无响应");
                }
            });

            return false;
        });
    });

});


/**
 * 获取店铺
 */
function getStoreList(form) {
    var param = {};
    $.ajax({
        url : storelist_url,
        type : 'post',
        dataType : 'json',
        contentType : "application/json; charset=utf-8",
        data : JSON.stringify(param),
    }).done(function(data) {
        var $item = "";
        var storeId = $('.storeId').val();
        for (var i in data){
            $item += "<option value='"+data[i].id+"' ";
            if(data[i].id == storeId){
                $item += "selected";
            }
            $item += " >" +data[i].name+ "</option>";
        }
        $('#store').append($item);

        // 重新渲染
        form.render('select');
    });
}